import request from "@/utils/request";

const STRATEGY_BASE_URL = "/ig/indicator";

const strategyManageAPI = {
  /** 连接表 */
  Join(data: IgIndicatorJoinParameter) {
    return request<any, TableInfoResponse>({
      url: `${STRATEGY_BASE_URL}/join`,
      method: "post",
      data: data,
    });
  },

  /** 新建指标 */
  insert(data: IndicatorInsertParameter) {
    return request<any, any>({
      url: `${STRATEGY_BASE_URL}/create`,
      method: "post",
      data: data,
    });
  },
  /** 根据指标新建指标  */
  InsertByIndicator(data?: IndicatorCreateBasedRequestDTO) {
    return request<any, any>({
      url: `${STRATEGY_BASE_URL}/insert`,
      method: "post",
      data: data,
    });
  },
  /** 查看sql */
  exmaineSql(data?: any) {
    return request<any, ExmaineSqlResponse>({
      url: `${STRATEGY_BASE_URL}/examine/sql`,
      method: "post",
      data: data,
    });
  },
};

export default strategyManageAPI;

/** 指标管理分页查询参数 */
export interface StrategyManagePageQuery extends PageQuery {}

export interface IgIndicatorJoinParameter {
  dataSourceId?: number;
  columns?: TableInfoConfig[];
  tables?: string[];
  joinType?: string[];
  joinColumns?: JoinColumnsConfig[];
}

export interface TableInfoConfig {
  tableName?: string;
  columnName?: string;
  isUnique?: boolean;
}

export interface JoinColumnsConfig {
  joinType?: string;
  table1?: string;
  table2?: string;
  column1?: string[];
  column2?: string[];
  isNotNull?: boolean;
  length?: number;
}

export interface TableInfoResponse {
  schema?: string;
  catalog?: string;
  tableName?: string;
  comment?: string;
  pkNames?: string[];
  indexInfoList?: indexInfo[];
  columns?: FieldColumnsConfig[];
}

export interface indexInfo {
  nonUnique?: boolean;
  indexName?: string;
  tableName?: string;
  schema?: string;
  catalog?: string;
  columnIndexInfoList?: columnIndexInfo[];
}

export interface columnIndexInfo {
  columnName?: string;
  ascOrDesc?: string;
}

export interface FieldColumnsConfig {
  tableName?: string;
  name?: string;
  type?: number;
  typeName?: string;
  size?: number;
  digit?: number;
  comment?: string;
  autoIncrement?: boolean;
  columnDef?: string;
  nullable?: boolean;
  typeEnum?: string;
  pk?: boolean;
}

export interface IndicatorInsertParameter {
  tableName?: string;
  indicators?: IndicatorCofig[];
}

export interface IndicatorCofig {
  indicatorName?: string;
  alias?: string;
  datasourceName?: string;
  datasourceId?: string;
  description?: string;
  domainId?: string;
  tag?: string;
  department?: string;
  subjectId?: number;
  aggregationMethod?: string;
  dataType?: string;
  size?: number;
  digit?: number;
  creator?: string;
  creatorId?: number;
  tableId?: number;
  createTime?: string;
  updateTime?: string;
  abnormalId?: number;
}

export interface IndicatorCreateBasedRequestDTO {
  indicators?: InsertByIndicatorDTO[];
}

export interface InsertByIndicatorDTO {
  /** 父指标id集合 */
  parentIds?: number[];
  /** 数据期列id集合 */
  timeColumnIds?: number[];
  /** 指标维度集合 */
  dimensionIds?: number[];
  /** 所述指标域 */
  domainId?: number;
  /** 所述分组 */
  domainGroupId?: number;
  /** 指标名称 */
  indicatroName?: string;
  /** 聚合方式 */
  aggregationMethod?: string;
  /** 创建人 */
  creator?: string;
  /** 异常规则id */
  abnormalId?: number;
}

export interface ExmaineSqlResponse {
  /** sql语句 */
  sql?: string;
}
